package chooong.integrate.recyclerView.snap

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import chooongg.base.widget.recyclerView.snap.GravityDelegate
import chooongg.base.widget.recyclerView.snap.GravitySnapHelper

class GravityPagerSnapHelper(gravity: Int, enableSnapLastItem: Boolean = false, snapListener: GravitySnapHelper.SnapListener? = null) : PagerSnapHelper() {

    private val delegate = GravityDelegate(gravity, enableSnapLastItem, snapListener)

    override fun attachToRecyclerView(recyclerView: RecyclerView?) {
        if (recyclerView != null
                && (recyclerView.layoutManager !is LinearLayoutManager || recyclerView.layoutManager is GridLayoutManager)) {
            throw  IllegalStateException("GravityPagerSnapHelper needs a RecyclerView with a LinearLayoutManager")
        }
        delegate.attachToRecyclerView(recyclerView)
        super.attachToRecyclerView(recyclerView)
    }

    override fun calculateDistanceToFinalSnap(layoutManager: RecyclerView.LayoutManager, targetView: View): IntArray {
        return delegate.calculateDistanceToFinalSnap(layoutManager, targetView)
    }

    override fun findSnapView(layoutManager: RecyclerView.LayoutManager): View? {
        return delegate.findSnapView(layoutManager)
    }

    fun enableLastItemSnap(snap: Boolean) {
        delegate.enableLastItemSnap(snap)
    }

    fun smoothScrollToPosition(position: Int) {
        delegate.smoothScrollToPosition(position)
    }

    fun scrollToPosition(position: Int) {
        delegate.scrollToPosition(position)
    }
}